// 模块化路由组件
import Vue from 'vue'
import VueRouter from 'vue-router'
// 明确的安装路由功能
Vue.use(VueRouter)

// import login from '@/views/login.vue'
// 创建路由模块和配置路由
const router = new VueRouter({
  // 使用routes进行路由配置
  routes: [{
      name: 'default',
      path: '/',
      redirect: {
        name: 'index',
        // 页面不缓存
        meta: {
          keepAlive: false
        }, //当前的.vue文件不需要缓存
      },
    },
    {
      name: 'login',
      path: '/login',
      component: () => import('@/views/login.vue')
    },
    {
      name: 'register',
      path: '/register',
      component: () => import('@/views/register.vue')
    },
    {
      name: 'personal',
      path: '/personal/:id',
      component: () => import('@/views/personal.vue')
    },
    {
      name: 'editUserInfo',
      path: '/editUserInfo/:id',
      component: () => import('@/views/editUserInfo.vue')
    },
    //搜索
    {
      name: 'Search',
      path: '/Search',
      component: () => import('@/views/search.vue')
    },
    // 新闻详情
    ,
    {
      name: 'articleDetail',
      path: '/articleDetail/:id',
      component: () => import('@/views/articleDetail.vue'),
      // 页面不缓存
      meta: {
        keepAlive: false
      }, //当前的.vue文件不需要缓存
    },
    // 首页
    {
      name: 'index',
      path: '/index/:id',
      component: () => import('@/views/index.vue')
    },
    //我的关注
    {
      name: 'follows',
      path: '/follows',
      component: () => import('@/views/myfollows.vue'),
      // 页面不缓存
      meta: {
        keepAlive: false
      }, //当前的.vue文件不需要缓存
    },
    {
      name: 'mysatr',
      path: '/mysatr',
      component: () => import('@/views/mysatr.vue'),
      // 页面不缓存
      meta: {
        keepAlive: false
      }, //当前的.vue文件不需要缓存
    },
    // 精彩评论页
    {
      name: 'Comment',
      path: '/Comment/:id',
      component: () => import('@/views/Comment.vue'),
      // 页面不缓存
      meta: {
        keepAlive: false
      }, //当前的.vue文件不需要缓存
    },
    // 栏目管理
    {
      name: 'cateManager',
      path: '/cateManager',
      component: () => import('@/views/cateManager.vue'),
      // 页面不缓存
      meta: {
        keepAlive: false
      }, //当前的.vue文件不需要缓存
    }

  ]
})
// 添加全局的导航守卫--前置守卫，在跳转之前触发的
// 它的作用是限制用户的路由跳转，说白了，就是当前的路由到底能不能跳转，我们在跳转之前进行判断并处理
router.beforeEach((to, from, next) => {

  // 我们需要判断一下用户当前目标路由是否是需要验证的路由
  let arr = ['/editUserInfo', '/personal']
  arr.map(v => {
    if (to.path.indexOf(v) !== -1) {
      // 判断用户是否有合法的合法的登陆状态，如果有则继续跳转，如果没有则重定向到登陆
      let token = localStorage.getItem('toutiao_59_token')
      if (token) {
        next()
      } else {
        next({
          name: 'login'
        })
      }
    } else {
      next()
    }
    return {

    }
  })


  // ${//to and from are Route Object,next() must be called to resolve the hook}

})

// 暴露

export default router